package com.example.app.domain.mqtt;

import net.sf.json.JSONObject;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.Random;

@Component
public class timetask {
    public static String getHttpInterface(String path) {
        BufferedReader in = null;
        StringBuffer result = null;
        try {
            URL url = new URL(path);
            //打开和url之间的连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Charset", "utf-8");
            connection.connect();

            result = new StringBuffer();
            //读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            return result.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return null;
    }

    @Scheduled(cron = "0/1 * * * * ?")
    public static void test() throws Exception {

        String str = getHttpInterface("http://127.0.0.1:8080/data/calculated");
        System.out.println(str);
//        JSONArray jsonArray = JSONObject.parseArray(str);
//        Object[] obj = str.toArray();
//        for (int i = 0; i < str.obj; i++)
//            Random random = new Random();
//
//
//        DecimalFormat df = new DecimalFormat("0.00");
        String[] arr1 = new String[7];
        String[] arr2 = new String[7];
        String[] arr3 = new String[6];
        String[] arr4 = new String[7];
//        for (int i = 0; i < 7; i++) {
//            double number = random.nextDouble() * (25 - 0);
//            arr1[i] = df.format(number);
//        }
//        for (int i = 0; i < 7; i++) {
//            double number = random.nextDouble() * (5 - 0);
//            arr2[i] = df.format(number);
//        }
//        for (int i = 0; i < 6; i++) {
//            double number = random.nextDouble() * (20 - 0);
//            arr3[i] = df.format(number);
//        }
//        for (int i = 0; i < 7; i++) {
//            double number = random.nextDouble() * (20 - 0);
//            arr4[i] = df.format(number);
//        }

//        for (int i = 0; i < 7; i++) {
//            System.out.println(arr1[i]);
//        }
//        System.out.println(arr1);
        ServerMQTT.sendMessage(arr1, arr2, arr3, arr4);
        System.out.println("定时任务~");
    }
//    public static void main(String[] args) throws Exception{
//        test();
//    }
}
